package exam.axhu.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import exam.axhu.entity.Course;
import exam.axhu.entity.CourseInfo;
import exam.axhu.service.CourseService;

@Service @Transactional
public class CourseServiceImpl implements CourseService{
//defaltBean: courseServiceImpl
	@Resource SessionFactory factory;
	
//course services
	
	public int addCourse(CourseInfo courseInfo) {
		if(this.findByCourseID(courseInfo.getCourseID())==null){
			factory.getCurrentSession().persist(courseInfo);
			return 0;   //set up a new course successfully
		}
		return 1;  //false, already has this course
	}


	public void deleteCourse(String... courseIDs) {
		for(String courseID : courseIDs){
			if(this.findByCourseID(courseID)!=null)
			{
				factory.getCurrentSession().delete(factory.getCurrentSession().load(CourseInfo.class,courseID));
				this.deleteWholeCourse(courseID);
			}
		}
	}

	public void updateCourse(CourseInfo courseInfo) {
		factory.getCurrentSession().merge(courseInfo);
		
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public CourseInfo findByCourseID(String courseID){
		return (CourseInfo) factory.getCurrentSession().get(CourseInfo.class,courseID);
	}
	
	
	@SuppressWarnings("unchecked")
	public List<CourseInfo> findByTeacherID(String teacherID) {
		Query query = factory.getCurrentSession().createQuery("from CourseInfo as c where c.teacherID = :searchID");
		query.setString("searchID", teacherID);
		if(query.list().size()!=0)
			return  query.list(); 
		else return null;
	}



// student services	
	
	public int addStudent(String courseID, String studentID) {
		if(this.searchStudent(courseID, studentID)==null){
			factory.getCurrentSession().persist(new Course(courseID, studentID));
			return 0;   //add a new student successfully
		}
		return 1;  //already has this record
	}

	public void deleteStudent(String courseID, String studentID) {
		if(this.searchStudent(courseID, studentID)!=null)
			factory.getCurrentSession().delete(factory.getCurrentSession().load(Course.class,this.searchStudent(courseID, studentID).getUniqueID()));			
	}
	
	public void deleteWholeCourse(String courseID){
		List<Course> courseItems = this.listStudentsByCourseID(courseID);
		for(Course item:courseItems)
			factory.getCurrentSession().delete(factory.getCurrentSession().load(Course.class,item.getUniqueID()));
	}
	
	public void updateStudent(String courseID, String studentID){
		
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Course searchStudent(String courseID, String studentID){
		Query query = factory.getCurrentSession().createQuery("from Course as c where c.courseID = :searchCourse and c.studentID = :searchStudent");
		query.setString("searchCourse", courseID);
		query.setString("searchStudent", studentID);
		if(query.list().size()!=0)
			return (Course) query.list().get(0); 
		else return null;
	}

	@SuppressWarnings("unchecked")
	public List<Course> listStudentsByCourseID(String courseID) {
		Query query = factory.getCurrentSession().createQuery("from Course as c where c.courseID = :searchCourse");
		query.setString("searchCourse", courseID);
		return query.list();
	}

	@SuppressWarnings("unchecked")
	public List<Course> listCoursesByStudentID(String studentID){
		Query query = factory.getCurrentSession().createQuery("from Course as c where c.studentID = :searchStudent");
		query.setString("searchStudent", studentID);
		return query.list();
	}
	
}
